package com.beyondsoft.common.activiti.support;

import com.beyondsoft.common.activiti.exception.ActivitiException;
import com.beyondsoft.common.activiti.exception.ResultCode;
import org.activiti.engine.ProcessEngine;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author zhangyan
 * @version $ Id: ClaimTaskSupport.java, v 0.1 2020/6/20 11:45 zhangyan Exp $
 */
@Component
public class ClaimTaskSupport {

    @Resource
    private ProcessEngine processEngine;

    /**
     * 认领
     *
     * @param taskId
     * @param userId
     */
    public void claim(String taskId, String userId) {
        if (StringUtils.isBlank(taskId) || StringUtils.isBlank(userId)) {
            throw new ActivitiException(ResultCode.NO_DATA.getCode(), "认领任务失败，任务号或认领人不能为空");
        }
        try {
            processEngine.getTaskService().claim(taskId, userId);
        } catch (Exception e) {
            throw new ActivitiException(ResultCode.NO_DATA.getCode(), "任务已被认领，请勿重复认领");
        }
    }


    /**
     * 取消认领
     *
     * @param taskId
     */
    public void unClaim(String taskId) {
        if (StringUtils.isBlank(taskId)) {
            throw new ActivitiException(ResultCode.NO_DATA.getCode(), "取消任务失败，任务号不能为空");
        }
        processEngine.getTaskService()
                .unclaim(taskId);
    }
}
